package baoxin.com.robotcoach;


import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.SimpleAdapter;

import com.iflytek.cloud.RecognizerResult;
import com.iflytek.cloud.SpeechError;
import com.iflytek.cloud.ui.RecognizerDialogListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import baoxin.com.robotcoach.utils.TtsListener;

public class MainActivity extends BottomActivity {

    private GridView gridView;
    private List<Map<String, Object>> dataList;
    private SimpleAdapter adapter;

    private RecognizerDialogListener mRecognizerDialogListener;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        setContentView(R.layout.activity_main);
        super.onCreate(savedInstanceState);
        gridView = findViewById(R.id.gridview);
        initData();

        String[] from = {"img", "text"};
        int[] to = {R.id.img, R.id.text};

        adapter = new SimpleAdapter(this, dataList, R.layout.gridview_item, from, to);

        gridView.setAdapter(adapter);

        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                switch (position) {
                    case 0:
                        Intent it = new Intent(MainActivity.this, PractiseActivity.class);
                        startActivity(it);
                        break;
                    case 1:
                        break;
                    case 4:
                        Intent intent = new Intent(MainActivity.this, VideoListActivity.class);
                        startActivity(intent);
                        break;
                    case 6:
                        Intent intent1 = new Intent(MainActivity.this, WeakActivity.class);
                        startActivity(intent1);
                        break;
                }
            }
        });

        mRecognizerDialogListener = new RecognizerDialogListener() {
            public void onResult(RecognizerResult results, boolean isLast) {


                final String result = TtsListener.printResult(results);
                if (isLast) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                     //       Toast.makeText(MainActivity.this,result,Toast.LENGTH_SHORT).show();
                            if (result.equals("挡位练习") || result.equals("档位练习")||result.equals("党委联系")
                                    ||result.equals("科目二练习")||result.equals("科目三练习")) {
                                Intent intent = new Intent(MainActivity.this, PractiseActivity.class);
                                startActivity(intent);
                            }
                        }
                    });
                }


            }

            /**
             * 识别回调错误.
             */
            public void onError(SpeechError error) {

            }

        };


    }



    @Override
    protected void onSpeak() {
        TtsListener.listener(this, mRecognizerDialogListener);
    }

    void initData() {
        int icon[] = {R.drawable.gears, R.drawable.light, R.drawable.wiper, R.drawable.integrated,
                R.drawable.video, R.drawable.intelligence, R.drawable.weak, R.drawable.help};
        String name[] = {"档位练习", "灯光练习", "雨刷器练习", "综合练习", "视频教学", "智能推荐", "薄弱分析", "帮助"};
        dataList = new ArrayList<>();
        for (int i = 0; i < icon.length; i++) {
            Map<String, Object> map = new HashMap<>();
            map.put("img", icon[i]);
            map.put("text", name[i]);
            dataList.add(map);
        }
    }
}
